# Python 2.6.4
# Project Euler, Problem 74
# Copyright 2010 Talha Zaman

def sumfac(n): return sum([fac[int(d)] for d in str(n)])

def cycle(n):
    if n<LIM:
        if loop[n]: return loop[n]
        loop[n] = 1 + rc(sumfac(n))
        return loop[n]
    else: return 1 + rc(sumfac(n))

fac = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
LIM = 2000000
loop = [0]*LIM
loop[0]=loop[1]=loop[2]=loop[145]=loop[40585]=1
loop[871]=loop[872]=loop[45361]=loop[45361]=2
loop[169]=loop[36301]=loop[1454]=3
sixty = 0
for i in range(1,1000000):
    if cycle(i)==60: sixty += 1
print sixty
